// pages/me/LP-register.js
var QQMapWX = require('../../dist/js/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'HLFBZ-EGZK6-MVQS6-ESMKT-T2G37-GIB2U'
});
var app = getApp();
var util = require('../../utils/util.js')
var _this;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //地址
    appLink: '', //app.globalData.appLink,
    // 链接判断
    buttonClicked: false,
    repSubmit: true, // 防重复提交
    index: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.init();
  },
  init:function(){
    wx.showLoading({
      title: '加载地址中...',
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    // 获取当前地址
    _this.setData({
      userInfo: app.globalData.programUserInfo,
      multiIndex: app.globalData.address.multiIndex,
      provinceName: app.globalData.address.provinceName,
      cityName: app.globalData.address.cityName,
      allArea: app.globalData.address.allArea,
      provinceAll: app.globalData.address.provinceAll,
      multiArray: app.globalData.address.multiArray,
      i: app.globalData.address.i
    })
    _this.changeAddress(_this.data.i);
    // wx.getStorage({
    //   key: 'allArea',
    //   success(res) {
    //     let provinceAll = res.data[0];
    //     let province = [];
    //     for (var i = 0; i < provinceAll.length; i++) {
    //       province.push(provinceAll[i].fullname);
    //     }
    //     _this.setData({
    //       allArea: res.data,
    //       provinceAll: res.data[0],
    //       multiArray: [province, []],
    //       multiIndex: [0, 0]
    //     })
    //     _this.getAddress();
    //   },
    //   fail(){
    //     _this.getCityList(); 
    //   }
    // })
  },
  onShareAppMessage: function () {

  },
  getCityList:function(){
    // 获取全部省市
    qqmapsdk.getCityList({
      success: function (res) {
        let provinceAll = res.result[0];
        let province = [];
        for (var i = 0; i < provinceAll.length; i++) {
          province.push(provinceAll[i].fullname);
        }
        _this.setData({
          allArea: res.result,
          provinceAll: res.result[0],
          multiArray: [province, []],
          multiIndex: [0, 0]
        })
        wx.setStorage({
          key: "allArea",
          data: res.result
        })
        _this.getAddress();
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  //获取当前地址
  getAddress:function(){
    // 获取当前地址
    wx.getLocation({
      success: function (res) {
        console.log(res);
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (rs) {
            let multiArray = _this.data.multiArray;
            let address = rs.result.address_component;
            for (var i = 0; i < multiArray[0].length; i++) {
              if (address.province == multiArray[0][i]) {
                _this.setData({
                  multiIndex: [i, 0],
                  provinceName: multiArray[0][i],
                  cityName: address.city
                })
                _this.changeAddress(i);
              }
            }
          }
        })
      },
    })
  },
  // 地址
  bindRegionChange: function (e) {
    _this.setData({
      region: e.detail.value
    })
  },
  //修改
  changeAddress:function(index){
    wx.hideLoading();
    let provinceAll = _this.data.provinceAll,
      id = provinceAll[index].id,
      multiArray = _this.data.multiArray;
    if (id == '110000' || id == '120000' || id == '310000' || id =='500000'){
      multiArray[1] = [provinceAll[index].fullname]
      _this.setData({
        multiArray: multiArray
      })
      
    }else{
      qqmapsdk.getDistrictByCityId({
        id: id, // 对应城市ID
        success: function (res) {
          let cityAll = res.result, citys = [];
          for (var i = 0; i < cityAll[0].length; i++){
            let city = cityAll[0][i].fullname;
            citys.push(city);
          }
          multiArray[1] = citys;
          _this.setData({
            multiArray: multiArray
          })
          console.log(res);
        },
        fail: function (res) {
          console.log(res);
        },
        complete: function (res) {
          console.log(res);
        }
      });
    }
  },
  pickerColumnChange(e){
    let column = e.detail.column,
      value = e.detail.value,
      provinceAll = _this.data.provinceAll;
    if (column == 0){
      _this.changeAddress(value);
    }
    console.log('column',e);
  },
  pickerChange(e) {
    let provinceIndex = e.detail.value[0],
      cityIndex = e.detail.value[1],
      provinceAll = _this.data.provinceAll,
      multiArray = _this.data.multiArray;
    _this.setData({
      provinceName: multiArray[0][provinceIndex],
      cityName: multiArray[1][cityIndex]
    })
  },
  // 输入信息
  massageChange: function (e) {
    var userType = e.currentTarget.dataset.type
    var massage = e.detail.value; // userLeaveMassage 留言
    switch (userType) {
      case 'name':
        _this.setData({ name: massage })
        break;
      case 'platform':
        _this.setData({ platform: massage })
        break;
      case 'lpGrade':
        _this.setData({ lpGrade: massage })
        break;
    }
  },
  // 验证信息
  verifyMessage: function (font) {
    var provinceName = _this.data.provinceName, // 地区
      name = _this.data.name, // 名字
      platform = _this.data.platform, // 平台
      lpGrade = _this.data.lpGrade; // LP
    // 判断是否有名字
    if (name == null || name == '') {
      util.modalMsg('提示', font + '请输入名字', function () {
        return false;
      }, null, false);
      return false;
    }
    // 判断是否有平台
    if (platform == null || platform == '') {
      util.modalMsg('提示', font + '请输入平台信息', function () {
        return false;
      }, null, false);
      return false;
    }
    // 判断是否有LP
    if (lpGrade == null || lpGrade == '') {
      util.modalMsg('提示', font + '请输入LP等级', function () {
        return false;
      }, null, false);
      return false;
    }
    // 判断是否有地区
    if (provinceName == null || provinceName == '') {
      util.modalMsg('提示', font + '请选择地区', function () {
        return false;
      }, null, false);
      return false;
    }
    return true;
  },
  submitFun: function () {
    if (!_this.verifyMessage('添加地址需要')) {
      return false;
    }
    // 防重复提交
    if (!_this.data.repSubmit) {
      return false;
    }
    _this.setData({
      repSubmit: false
    })
    var data = {};
    var region = _this.data.region; // 地区
    data.userId = _this.data.userInfo.id.toString();
    data.userName = _this.data.name; // 名字
    data.platform = _this.data.platform; // 平台
    data.lpLevel = _this.data.lpGrade; // 等级
    data.province = _this.data.provinceName;
    data.city = _this.data.cityName;
    app.getMassage(data, null, 'saveLpInfo', function (res) {
      if (res.status == 'success') {
        util.toastMsg('登记成功', 'success');
        var pages = getCurrentPages(),
          currPage = pages[pages.length - 2]; //上一个页面
        currPage.init();
        // _this.setData({
        //   successPop: true
        // })
      } else {
        util.modalMsg('提示', res.message, function () {
          setTimeout(function () {
            if (!_this.data.buttonClicked) {
              util.buttonClicked(_this);
              wx.navigateBack({
                delta: 1
              })
            }
          }, 1000)
          return false;
        }, null, false);
      }
    })
  },
  // 确定，返回上一页
  goBack:function(){
    // 刷新上一页
    var page = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 上一页
    prevPage.init();
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: 1
      })
    }
  }
})